/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 数据字典状态管理
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/10/14
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191014, 添加注释)
 */

import {findSysDicts,addSysDict,updSysDict,batchDelSysDicts,loadSorts} from 'api/sysDictApi';
import {loadAlgorithmSort} from 'api/arithmeticDict'
import dict from '@/libs/dict';

/* 数据字典的状态管理 */
const sysDict = {
    state: {
        flag: false,
        sysDictDTOs: [],
        sorts:[],
        page: {
            currentPage: 1,
            pageSize: 5,
            total: 0
        },
        arithmeticSorts: []
    },
    mutations: {
        findSysDictsMu(state, obj) {
            if (obj.rows !== '') {
                state.sysDictDTOs = obj.rows;
                state.page.currentPage = obj.currentPage;
                state.page.pageSize = obj.pageSize;
                state.page.total = obj.total;
            }
        },
        loadSortsMu(state, obj){
          state.sorts = obj;
        },
        loadArithmeticSorts(state, obj) {
            state.arithmeticSorts = obj;
        }
    },
    actions: {
        /*获取数据字典列表数据请求*/
        findSysDictsAct (context, para) {
          findSysDicts(para).then(response => {
                let data = response.data;
                context.commit('findSysDictsMu', data);
            });
        },
        /*添加数据字典*/
        addSysDictAct (context, para) {
            return addSysDict(para).then(response => {
                let data = response.data;
                return data;
            });
        },
        /*更新数据字典信息*/
        updSysDictAct (context, para) {
            return updSysDict(para).then(response => {
                let data = response.data;
                return data;
            });
        },
        /*批量删除数据字典*/
        batchDelSysDictsAct (context, para) {
            return batchDelSysDicts(para).then(response => {
                let data = response.data;
                return data;
            });
        },
        loadSortsAct(context){
              return loadSorts().then(response=>{
                let data = response.data;
                let newData = []
                  data.forEach(item=>{
                      if(item.value === dict.sortCode.unitNumber || item.value === dict.sortCode.FILL_QKEY_TYPE
                        || item.value === dict.sortCode.CONTAINER_TYPE || item.value === dict.sortCode.AGREEMENT_TYPE){
                          newData.push(item)
                      }
                  })
                context.commit('loadSortsMu',data);
              })
        },
        loadAlgorithmSortList(context, para){
            return loadAlgorithmSort(para).then(response=>{
                let data = response.data;
                context.commit('loadArithmeticSorts',data);
            })
        }
    }
};

export default sysDict;
